#!/bin/bash
export PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:{{ansible_env["PATH"]}}
BK_USER={{mysql_backup_user}}
BK_PASS="{{mysql_backup_pass}}"
IP="{{ansible_default_ipv4.address}}"
mail_script=$(cd $(dirname $0) && pwd)/send_mail.py
project_name="{{group_names[-1]}} ${IP}"
mysql_sock={{mysql_sock}}
mysql_host=localhost
mysql_vip={{mysql_vip}}
mysql_backup_host={{mysql_backup_host}}
mysql_basedir={{mysql_basedir.msg}}
mysql_datadir={{mysql_datadir.msg}}
backup_num={{mysql_backup_num}}
BackPath={{mysql_backup_path}}
Remote_BackPath={{mysql_backup_remote_path}}/$IP
mysql_backup_remote_user={{mysql_backup_remote_user}}
REMOT_HOST={{mysql_backup_remote_host}}

lock_file={{mysql_var_dir}}/.mysql_backup.lock

{%raw%}
exec 3<> ${lock_file}
if flock -n 3;then
    #echo "get ${lock_file} lock file."
    :
else
    echo "get lock file failed. now exit."
    mail_text="mysql_backup ${project_name} get lock file failed. now exit."
    python ${mail_script} "${project_name}" "${mail_text}"
    exit 5
fi

day_dt=$(date +%F)
#BackPath=/data/apps/data/mysqlbackup
OUT_FILE=/tmp/`basename $0`.out
#Remote_BackPath=/data2/mysqlbackup/$IP
# 如果 vip 是空的，那么就往下继续执行
# 如果不为空，则判断本机是否有 vip
check_vip(){
    if [ -n "${mysql_vip}" ];then
        # vip 不为空, 判断 vip 是否存在于本机
        if ip a |grep "${mysql_vip}" &> /dev/null;then
            echo "the host isn't backup host."
            exit 5
        else
            echo "continue"
        fi 
    else
        # vip 为空的，
        vip_text="VIP is none." 
        echo "${vip_text}"
    fi
}
check_backup_host(){
    if [ -n "${mysql_backup_host}" ];then
        # ${mysql_backup_host} 不为空, 判断 ip 是否是本机
        if ip a |grep "${mysql_backup_host}" &> /dev/null;then
            echo "continue"
        else
            echo "the host isn't backup host."
            exit 5
        fi 
    else
        # vip 为空的，
        vip_text="mysql_backup_host is none." 
        echo "${vip_text}"
    fi
}

check_vip
check_backup_host

[ -d ${BackPath} ] || mkdir -p ${BackPath}
# 判断备份目录所在的磁盘空间是否充裕
datadir_size=$(du -s ${mysql_datadir} |awk '{print $1}')
# datadir 的大小再加上 100G
datadir_size=$[datadir_size+104857600]
BackPath_dev_num=$(stat -c '%d' ${BackPath})
all_mount_point=($(df -l |awk '{print $NF}' | tail -n +2))
for p in ${all_mount_point[@]};do
    p_dev_num=$(stat -c '%d' ${p})
    if [ ${BackPath_dev_num} == ${p_dev_num} ];then
        mount_dev=${p}
        break
    else
        continue
    fi
done
dev_free_space=$(df -l ${mount_dev} | tail -n 1 |awk '{print $4}')
if [ ${datadir_size} -lt ${dev_free_space} ];then
    echo "${mount_dev} space is ok."
else
    echo "the ${mount_dev} free space is unavailable."
    mail_text="`date` ${project_name} mysql backup failed. the ${mount_dev} free space is unavailable."
    echo "backup failed." >> $OUT_FILE
    python ${mail_script} "${project_name}" "${mail_text}"
    exit 6
fi

# 确定配置文件
if [[ ${mysql_basedir} == /data/apps/opt/mysql* ]] || [[ ${mysql_basedir} == /data1/apps/opt/mysql* ]];then
    mysql_conf=/data1/apps/config/mysql/my.cnf
    if [ -e ${mysql_conf} ];then
        :
    else
        mysql_conf=/etc/my.cnf
    fi
elif [[ ${mysql_basedir} == /usr* ]];then
    mysql_conf=/etc/my.cnf
else
    if [ -e /etc/my.cnf ];then
        mysql_conf=/etc/my.cnf
    else
        mysql_conf=${mysql_basedir}/my.cnf
    fi
fi
# 开始备份
echo -e "--------------------------------\n`date` start mysql backup!" >> $OUT_FILE

innobackupex --defaults-file=${mysql_conf} --user=${BK_USER} --password="${BK_PASS}" --socket=${mysql_sock} --host=${mysql_host} ${BackPath} 
backup_stat=$?
#backup_stat=0

if [ ${backup_stat} == 0 ];then
    backup_file=$(ls ${BackPath}/${day_dt}* -d |tail -n 1)
else
    mail_text="`date` ${project_name} mysql backup failed."
    echo "backup failed." >> $OUT_FILE
    python ${mail_script} "${project_name}" "${mail_text}"
    exit 5
fi
cd ${BackPath}
if ssh ${mysql_backup_remote_user}@${REMOT_HOST} [ ! -e ${Remote_BackPath} ];then
    ssh ${mysql_backup_remote_user}@${REMOT_HOST} mkdir ${Remote_BackPath}
fi
scp -r ${backup_file} ${mysql_backup_remote_user}@${REMOT_HOST}:${Remote_BackPath}/
scp_stat=$?
#scp_stat=0

if [ ${scp_stat} == 0 ];then
    scp_text="send mysql backup file successed."
else
    scp_text="send mysql backup file failed."
fi

# delete old file
cd ${BackPath}
echo "rm -rf ${old_file}"
#old_file=$(ls -c1 |tail -n +2)
old_file=$(find ${BackPath} -maxdepth 1 -type d -mtime +10)
rm -rf ${old_file}
# 删除远程备份目录的旧的备份

remote_old_file=$(ssh ${mysql_backup_remote_user}@${REMOT_HOST} "cd ${Remote_BackPath}; ls -c1 |tail -n +$[backup_num+10]")
remote_old_file=$(echo ${remote_old_file} | tr "\n" " ")
echo ${remote_old_file}
ssh -T ${mysql_backup_remote_user}@${REMOT_HOST} << EOF
   cd ${Remote_BackPath}
   rm -rf ${remote_old_file} 
EOF

mail_text="
`date` ${project_name} mysql database backup successed.
backup file is ${backup_file}.
backup file info: 
`stat ${backup_file}`.
backup file size: `du -sh ${backup_file}`.
${scp_text}
the VIP is: ${mysql_vip}. ${vip_text}.
deleted ${old_file}.
deleted ${REMOT_HOST}:${remote_old_file}
"
python ${mail_script} "${project_name}" "${mail_text}"
{%endraw%}
